﻿/*舒彩光
*2009-12-27
*仿微软的日历控件
*功能：为文本框选择日期
*除三个基础js外，还需要Date.js,Action.js提供支持。
*/
Function.registerNameSpace("Page");
Page.Calendar=function(obj,format){
	Function.registerClass(this);
	//状态值
	this._SHOWMODE_FORINPUT_	= "input";
	this._SHOWMODE_FOROTHER_	= "other";
	this._CALENDAR_DAYS_		= "days";
	this._CALENDAR_MONTHS_		= "months";
	this._CALENDAR_YEARS_		= "years";

	this._today=new Date();
	this._selectedDate=new Date();//选中的日期
	this._visibleDate=new Date(this._selectedDate.getFullYear(),this._selectedDate.getMonth(),1);//日历显示的日期的第一天
	this._actioning=false;
	var dtf=document.CultureInfo.CurrentCulture.dateTimeFormat;
	this._firstDayOfWeek=dtf.FirstDayOfWeek;
	this._weekDayNames=dtf.ShortestDayNames;
	this._monthsNames=dtf.AbbreviatedMonthNames;
	this._isShowing=false;
	this._enableYears=null;//可以选择的年份
    this._enableMonths=null;//可以选择的月份
    this._enableDays=null;//可以选择的日期
    this._enableDates=null;//可以选择的日期
	
	if(typeof(obj)=="string") obj=$$(obj);
	this.object=obj;
	//生成日历总框架
	this._pageCalendar=null;
	if(obj.tagName.toLowerCase()=="input"){
		this._pageCalendar=document.addElement(document.body,"div");
		document.setStyle(this._pageCalendar,{position:"absolute",display:"none",zIndex:99999});
		this._showMode=this._SHOWMODE_FORINPUT_;
		this._openHandler=EventOperate.createDelegate(this,function(sender,e){this.Show();});
		this._preCloseHandler=EventOperate.createDelegate(this,function(sender,e){this._closeTimeleg.run();});
		this._closeHandler=Function.createDelegate(this,function(sender,e){this.Hide();});
		EventOperate.addEvent(this.object,"focus",this._openHandler);
		EventOperate.addEvent(this.object,"blur",this._preCloseHandler);
		
		this._closeTimeleg=new Function.Timelag(this._closeHandler,400);
		
		if(format){
			this._format=format;
			if(this.object.value.length>0){
				this._selectedDate=Date.parse(this.object.value,format);
			}
		}else{
			if(this.object.value.length>0){
				this._selectedDate=Date.parse(this.object.value);
			}
		}
		if(this._selectedDate!=null) this._visibleDate=new Date(this._selectedDate.getFullYear(),this._selectedDate.getMonth(),1);
		else{
	        this._selectedDate=new Date();
	        this._visibleDate=new Date(this._selectedDate.getFullYear(),this._selectedDate.getMonth(),1);
		}
	}else{
		this._pageCalendar=document.addElement(obj,"div");
		document.setStyle(this._pageCalendar,{position:"relative"});
		this._showMode=this._SHOWMODE_FOROTHER_;
		this._isShowing=true;
	}
	this._pageCalendar.className="AGuang___Page___Calendar";
	this._titleBar=document.addElementAtEnd(this._pageCalendar,"div");
	this._titleBar.className="title";
	this._titleBar.innerHTML="舒彩光";
	this._prevButton=document.addElementAtEnd(this._pageCalendar,"div");
	this._prevButton.className="prev";
	this._nextButton=document.addElementAtEnd(this._pageCalendar,"div");
	this._nextButton.className="next";
	this._container=document.addElementAtEnd(this._pageCalendar,"div");
	this._container.className="Container"
	this._inBox=document.addElementAtEnd(this._container,"div");
	document.setStyle(this._inBox,{height:"100%",overflow:"hidden",position:"relative"});
	this._footer=document.addElementAtEnd(this._pageCalendar,"div");
	this._footer.className="footer";
	this._footer.innerHTML="今天:"+this._today.toString("MMMM dd, yyyy");

	//事件
	this._titleBar_events={
		mouseover:EventOperate.createDelegate(this,this.__titleBar_mouseover),
		mouseout:EventOperate.createDelegate(this,this.__titleBar_mouseout),
		click:EventOperate.createDelegate(this,this.__titleBar_click)
	};
	this._prevButton_events={
		mouseover:EventOperate.createDelegate(this,this.__prevButton_mouseover),
		mouseout:EventOperate.createDelegate(this,this.__prevButton_mouseout),
		click:EventOperate.createDelegate(this,this.__prevButton_click)
	};
	this._nextButton_events={
		mouseover:EventOperate.createDelegate(this,this.__nextButton_mouseover),
		mouseout:EventOperate.createDelegate(this,this.__nextButton_mouseout),
		click:EventOperate.createDelegate(this,this.__nextButton_click)
	};
	this._footer_events={
		//mouseover:EventOperate.createDelegate(this,this.__footer_mouseover),
		//mouseout:EventOperate.createDelegate(this,this.__footer_mouseout),
		click:EventOperate.createDelegate(this,this.__footer_click)
	};
	this._td_events={
		mouseover:EventOperate.createDelegate(this,this.__td_mouseover),
		mouseout:EventOperate.createDelegate(this,this.__td_mouseout),
		click:EventOperate.createDelegate(this,this.__td_click)
	};
	EventOperate.addEvents(this._titleBar,this._titleBar_events);
	EventOperate.addEvents(this._prevButton,this._prevButton_events);
	EventOperate.addEvents(this._nextButton,this._nextButton_events);
	EventOperate.addEvents(this._footer,this._footer_events);
	EventOperate.FixedPart(this._pageCalendar);

	this._buildDaysTable();
	this._fillDays();
	this._currentTable=this._daysTable;
	this._calendarMode=this._CALENDAR_DAYS_;
	if(this._showMode==this._SHOWMODE_FOROTHER_) document.layerGoodInIE6(this._pageCalendar);
	this._closeAction=new Page.Action(this._pageCalendar);
}

//日历展示日期的表格
Page.Calendar.prototype._buildDaysTable=function(){
	this._daysTable=document.addElementAtEnd(this._inBox,"table");
	this._daysTable.cellSpacing=0;
	this._daysTable.cellPadding=0;
	this._daysTable.border=0;
	this._daysTable.className="Calender";
	document.setStyle(this._daysTable,{left:0,top:0,position:"absolute"});
	this._daysTableHead=this._daysTable.createTHead();
	this._daysTableHeadRow=this._daysTableHead.insertRow(0);
	for(var i=0;i<7;i++){
		document.addElementAtEnd(this._daysTableHeadRow,"th").innerHTML=this._weekDayNames[(i+this._firstDayOfWeek) % 7];
	}
	this._daysTableBody=document.addElementAtEnd(this._daysTable,"tbody");
	for(var i=0;i<6;i++){
		var row=this._daysTableBody.insertRow(i);
		for(var j=0;j<7;j++){
			var cell=row.insertCell(j);
			EventOperate.addEvents(cell,this._td_events);
		}
	}
}

//日历展示月份的表格
Page.Calendar.prototype._buildMonthsTable=function(){
	this._monthsTable=document.addElementAtEnd(this._inBox,"table");
	this._monthsTable.cellSpacing=0;
	this._monthsTable.cellPadding=0;
	this._monthsTable.border=0;
	this._monthsTable.className="Calender";
	document.setStyle(this._monthsTable,{left:0,top:0,position:"absolute"});
	for(var i=0;i<4;i++){
		var row=this._monthsTable.insertRow(i);
		for(var j=0;j<3;j++){
			var cell=row.insertCell(j);
			cell.innerHTML=this._monthsNames[(i*3)+j];
			cell.width="33.3%";
			EventOperate.addEvents(cell,this._td_events);
		}
	}
}

//日历展示年份的表格
Page.Calendar.prototype._buildYearsTable=function(){
	this._yearsTable=document.addElementAtEnd(this._inBox,"table");
	this._yearsTable.cellSpacing=0;
	this._yearsTable.cellPadding=0;
	this._yearsTable.border=0;
	this._yearsTable.className="Calender";
	document.setStyle(this._yearsTable,{left:0,top:0,position:"absolute"});
	for(var i=0;i<4;i++){
		var row=this._yearsTable.insertRow(i);
		for(var j=0;j<3;j++){
			var cell=row.insertCell(j);
			cell.width="33.3%";
			EventOperate.addEvents(cell,this._td_events);
		}
	}
}

//填充日期表格
Page.Calendar.prototype._fillDays=function(){
	this._titleBar.innerHTML=this._visibleDate.toString("MMMM, yyyy");
	var daysToBacktrack=this._visibleDate.getDay()-this._firstDayOfWeek;
	if(daysToBacktrack<=0) daysToBacktrack+=7;
	var currentDate=new Date(this._visibleDate.getFullYear(),this._visibleDate.getMonth(),1-daysToBacktrack);
	for(var i=0;i<6;i++){
		for(var j=0;j<7;j++){
			var cell=this._daysTableBody.rows[i].cells[j];
			var currentDay=currentDate.getDate();
			var currentMonth=currentDate.getMonth();
			var currentYear=currentDate.getFullYear();
			document.removeClass(cell);
			if(currentMonth!=this._visibleDate.getMonth()) document.addClass(cell,"other");
			else{
				if(currentYear==this._today.getFullYear()
				&&currentMonth==this._today.getMonth()
				&&currentDay==this._today.getDate())
				document.addClass(cell,"today");
				if(currentYear==this._selectedDate.getFullYear()
				&&currentMonth==this._selectedDate.getMonth()
				&&currentDay==this._selectedDate.getDate()) 
				document.addClass(cell,"active");
			}
			cell.innerHTML=currentDay;
			cell.title=currentDate.toString("D");
			cell.date=currentDate;
			currentDate=new Date(currentYear,currentMonth,currentDay+1);
		}
	}
}

Page.Calendar.prototype._fillMonths=function(){
	var currentDate=new Date(this._visibleDate.getFullYear(),0,1);
	this._titleBar.innerHTML=this._visibleDate.toString("yyyy");
	for(var i=0;i<4;i++){
		for(var j=0;j<3;j++){
			var cell=this._monthsTable.rows[i].cells[j];
			var currentMonth=currentDate.getMonth();
			var currentYear=currentDate.getFullYear();
			document.removeClass(cell);
			if(currentYear==this._today.getFullYear()&&currentMonth==this._today.getMonth())
				document.addClass(cell,"today");
			if(currentYear==this._selectedDate.getFullYear()&&currentMonth==this._selectedDate.getMonth()) 
				document.addClass(cell,"active");
			cell.date=currentDate;
			cell.title=currentDate.toString("MMMM, yyyy");
			currentDate=new Date(currentYear,currentMonth+1,1);
		}
	}
}

Page.Calendar.prototype._fillYears=function(){
	var startYear=Math.floor(this._visibleDate.getFullYear()/10)*10;
	var endYear=startYear+9;
	var currentDate=new Date(startYear-1,0,1);
	this._titleBar.innerHTML=startYear+"-"+endYear;
	for(var i=0;i<4;i++){
		for(var j=0;j<3;j++){
			var cell=this._yearsTable.rows[i].cells[j];
			var currentYear=currentDate.getFullYear();
			document.removeClass(cell);
			if(currentYear<startYear||currentYear>endYear) document.addClass(cell,"other");
			else{
				if(currentYear==this._today.getFullYear()) document.addClass(cell,"today");
				if(currentYear==this._selectedDate.getFullYear()) document.addClass(cell,"active");
			}
			cell.innerHTML=currentDate.toString("yyyy");
			cell.date=currentDate;
			currentDate=new Date(currentYear+1,0,1);
		}
	}
}

Page.Calendar.prototype._setEnableData=function(){
	switch(this._calendarMode){
		case this._CALENDAR_DAYS_:
			for(var i=0;i<this._daysTableBody.rows.length;i++){
			    var row=this._daysTableBody.rows[i];
			    for(var j=0;j<row.cells.length;j++){
			        var cell=row.cells[j];
			        document.removeClass(cell,"enable");
			        if(this._enableDays) if(this._enableDays.indexOf(cell.date.getDate())!=-1&&this._visibleDate.getMonth()==cell.date.getMonth()) document.addClass(cell,"enable");
			        if(this._enableDates) if(this._compareData(cell.date,this._enableDates,"yyyy-MM-dd")) document.addClass(cell,"enable");
			    }
			}
			break;
		case this._CALENDAR_MONTHS_:
			for(var i=0;i<this._daysTableBody.rows.length;i++){
			    var row=this._daysTableBody.rows[i];
			    for(var j=0;j<row.cells.length;j++){
			        var cell=row.cells[j];
			        if(this._enableMonths) if(this._enableMonths.indexOf(cell.date.getMonth()+1)!=-1&&this._visibleDate.getFullYear()==cell.date.getFullYear()) document.addClass(cell,"enable");
			        if(this._enableDates) if(this._compareData(cell.date,this._enableDates,"yyyy-MM")) document.addClass(cell,"enable");
			    }
			}
			break;
		case this._CALENDAR_YEARS_:
			for(var i=0;i<this._daysTableBody.rows.length;i++){
			    var row=this._daysTableBody.rows[i];
			    for(var j=0;j<row.cells.length;j++){
			        var cell=row.cells[j];
			        if(this._enableYears) if(this._enableYears.indexOf(cell.date.getFullYear())!=-1) document.addClass(cell,"enable");
			        if(this._enableDates) if(this._compareData(cell.date,this._enableDates,"yyyy")) document.addClass(cell,"enable");
			    }
			}			
			break;
	}
}

Page.Calendar.prototype._compareData=function(date,arr,format){
    for(var i=0,l=arr.length;i<l;i++){
        if(arr[i].toString(format)==date.toString(format)) return true;
    }
    return false;
}

Page.Calendar.prototype._getSelectedDate=function(){
	return this._today;
}

//显示日历，并根据文本框内的日期初始化日历
Page.Calendar.prototype.Show=function(){
    if(this._isShowing) return;
	document.layerGoodInIE6(this._pageCalendar);
	if(this._closeAction.IsPlaying()) this._closeAction.StyleActionStop();
	this._pageCalendar.style.display="none";
	this._pageCalendar.style.display="";
	if(this._showMode==this._SHOWMODE_FORINPUT_){
		//var left=document.getObjLeft(this.object);
		//var top=document.getObjTop(this.object);
		//var h=this.object.offsetHeight;
		//document.setStyle(this._pageCalendar,{left:left+"px",top:(top+h)+"px"});
		this._closeAction.AutoSide(this.object);
		this._isShowing=true;
	}
    this._closeAction.AddNumericStyleAction("opacity",0.4,1,null);
    var closeHandler=Function.createDelegate(this,function(sender,e){
        this._closeAction.Remove_StopedHandler(closeHandler);
    });
    this._closeAction.Add_StopedHandler(closeHandler);
    this._closeAction.StyleActionPlay(0.13);
}

//更改日历控件的样式
Page.Calendar.prototype.SetCssClass=function(className){
    this._pageCalendar.className=className;
}

//隐藏日历
Page.Calendar.prototype.Hide=function(){
    if(!this._isShowing) return;
    if(this._closeAction.IsPlaying()) this._closeAction.StyleActionStop();
    this._closeAction.AddNumericStyleAction("opacity",0.8,0.1,null);
    var closeHandler=Function.createDelegate(this,function(sender,e){
        this._pageCalendar.style.display="none";
        this._closeAction.Remove_StopedHandler(closeHandler);
        this._isShowing=false;
    });
    this._closeAction.Add_StopedHandler(closeHandler);
    this._closeAction.StyleActionPlay(0.15);
}

//设置可以选择的年份
Page.Calendar.prototype.SetEnableYears=function(arr){
	this._enableYears=arr;
	this._setEnableData();
}
Page.Calendar.prototype.SetEnableYearsArea=function(start,end){
    this._enableYears=[];
    for(var i=start;i<=end;i++){
        this._enableYears.push(i);
    }
    this._setEnableData();
}

//设置可以选择的月份(1-12)
Page.Calendar.prototype.SetEnableMonths=function(arr){
    this._enableMonths=arr;
    this._setEnableData();
}
Page.Calendar.prototype.SetEnableMonthsArea=function(start,end){
    this._enableMonths=[];
    for(var i=start;i<=end;i++){
        this._enableMonths.push(i);
    }
    this._setEnableData();
}

//设置可以选择的日期(1-31)
Page.Calendar.prototype.SetEnableDays=function(arr){
    this._enableDays=arr;
    this._setEnableData();
}
Page.Calendar.prototype.SetEnableDaysArea=function(start,end){
    this._enableDays=[];
    for(var i=start;i<=end;i++){
        this._enableDays.push(i);
    }
    this._setEnableData();
}

//设置可以选择的日期(日期类型);
Page.Calendar.prototype.SetEnableDates=function(arr){
    this._enableDates=arr;
    this._setEnableData();
}
Page.Calendar.prototype.SetEnableDatesArea=function(start,end){
    this._enableDates=[];
    for(var i=start;i<=end;i=new Date(i.getFullYear(),i.getMonth(),i.getDate()+1)){
        this._enableDates.push(i);
    }
    this._setEnableData();
}

//10年改变时
Page.Calendar.prototype.Add_YearsChange=function(func){
    EventOperate.addHandler(this,"YearsChange",func);
}
Page.Calendar.prototype.Remove_YearsChange=function(func){
    EventOperate.removeHandler(this,"YearsChange",func);
}
Page.Calendar.prototype._onYearsChange=function(){
    var func=EventOperate.getHandler(this,"YearsChange");
    var e={date:this._visibleDate}
	if(func) func(this,e);
}
//年改变时
Page.Calendar.prototype.Add_YearChange=function(func){
    EventOperate.addHandler(this,"YearChange",func);
}
Page.Calendar.prototype.Remove_YearChange=function(func){
    EventOperate.removeHandler(this,"YearChange",func);
}
Page.Calendar.prototype._onYearChange=function(){
    var func=EventOperate.getHandler(this,"YearChange");
    var e={date:this._visibleDate};
	if(func) func(this,e);
}
//月改变时
Page.Calendar.prototype.Add_MonthChange=function(func){
    EventOperate.addHandler(this,"MonthChange",func);
}
Page.Calendar.prototype.Remove_MonthChange=function(func){
    EventOperate.removeHandler(this,"MonthChange",func);
}
Page.Calendar.prototype._onMonthChange=function(){
    var func=EventOperate.getHandler(this,"MonthChange");
    var e={date:this._visibleDate};
	if(func) func(this,e);
}
//日改变时
Page.Calendar.prototype.Add_DayChange=function(func){
    EventOperate.addHandler(this,"DayChange",func);
}
Page.Calendar.prototype.Remove_DayChange=function(func){
    EventOperate.removeHandler(this,"DayChange",func);
}
Page.Calendar.prototype._onDaySelected=function(){
	if(this._showMode==this._SHOWMODE_FORINPUT_){
		if(this._format) this.object.value=this._selectedDate.toString(this._format);
		else this.object.value=this._selectedDate.toString("yyyy-MM-dd");
		this.Hide();
	}
	var func=EventOperate.getHandler(this,"DayChange");
    var e={date:this._selectedDate};
	if(func) func(this,e);
}

Page.Calendar.prototype.dispose=function(){
	EventOperate.clearEvents(this._titleBar);
	EventOperate.clearEvents(this._prevButton);
	EventOperate.clearEvents(this._nextButton);
	EventOperate.clearEvents(this._footer);
	delete this._titleBar_events;this._titleBar_events=null;
	delete this._prevButton_events;this._prevButton_events=null;
	delete this._nextButton_events;this._nextButton_events=null;
	delete this._footer_events;this._footer_events=null;
	document.delElement(this._pageCalendar);
	delete this._titleBar;this._titleBar=null;
	delete this._prevButton;this._prevButton=null;
	delete this._nextButton;this._nextButton=null;
	delete this._footer;this._footer=null;
	delete this._container;this._container=null;
	delete this._pageCalendar;this._pageCalendar=null;
	Function.recycleClass(this);
}

//前进或后退一格（日，月，年）
Page.Calendar.prototype._prevOrNext=function(value){
	switch(this._calendarMode){
		case this._CALENDAR_DAYS_:
			var visiblieDate=new Date(this._visibleDate.getFullYear(),this._visibleDate.getMonth()+value,this._visibleDate.getDate());
			break;
		case this._CALENDAR_MONTHS_:
			var visiblieDate=new Date(this._visibleDate.getFullYear()+value,this._visibleDate.getMonth(),this._visibleDate.getDate());
			break;
		case this._CALENDAR_YEARS_:
			var visiblieDate=new Date(this._visibleDate.getFullYear()+value*10,this._visibleDate.getMonth(),this._visibleDate.getDate());
			break;
	}
	this._prevOrNextAction(visiblieDate)
}

Page.Calendar.prototype._prevOrNextAction=function(newVisiblieDate){
	if(this._actioning) return;
	var newElement=this._currentTable;
	var oldElement=newElement.cloneNode(true);
	this._inBox.appendChild(oldElement);
	var action1=new Page.Action(newElement);
	var action2=new Page.Action(oldElement);
	var action1_handler=Function.createDelegate(this,function(sender,e){
		this._actioning=false;
		action1.Remove_StopedHandler(action1_handler);
		delete newElement;newElement=null;
		delete action1_handler;action1_handler=null;
    	action1.dispose();
		delete action1;action1=null;
	});
	var action2_handler=Function.createDelegate(this,function(sender,e){
	    for(var i=oldElement.rows.length-1;i>=0;i--) oldElement.deleteRow(i);
		oldElement.deleteTHead();
		this._inBox.removeChild(oldElement);
		action2.Remove_StopedHandler(action2_handler);
		delete oldElement;oldElement=null;
		delete action2_handler;action2_handler=null;
    	action2.dispose();
		delete action2;action2=null;
	});
	action1.Add_StopedHandler(action1_handler);
	action2.Add_StopedHandler(action2_handler);
	if(newVisiblieDate<this._visibleDate){
		document.setStyle(newElement,{left:(-newElement.offsetWidth)+"px",top:0,position:"absolute"});
		action1.AddNumericStyleAction("left",-newElement.offsetWidth,0,"px");
		document.setStyle(oldElement,{left:0,top:0,position:"absolute"});
		action2.AddNumericStyleAction("left",0,newElement.offsetWidth,"px");
	}else{
		document.setStyle(newElement,{left:newElement.offsetWidth+"px",top:0,position:"absolute"});
		action1.AddNumericStyleAction("left",newElement.offsetWidth,0,"px");
		document.setStyle(oldElement,{left:0,top:0,position:"absolute"});
		action2.AddNumericStyleAction("left",0,-newElement.offsetWidth,"px");
	}
	this._visibleDate=newVisiblieDate;
	switch(this._calendarMode){
		case this._CALENDAR_DAYS_:
			this._fillDays();
			this._onMonthChange();//触发月改变事件
			break;
		case this._CALENDAR_MONTHS_:
			this._fillMonths();
			this._onYearChange();//触发年改变事件
			break;
		case this._CALENDAR_YEARS_:
			this._fillYears();
			this._onYearsChange();//触发10年改变事件
			break;
	}
	this._setEnableData();//重设可选日期
	action1.StyleActionPlay(0.16);
	action2.StyleActionPlay(0.16);
}

Page.Calendar.prototype._changeCalenderMode=function(mode){
	switch(mode){
		case this._CALENDAR_DAYS_:
			if(!this._daysTable) this._buildDaysTable();
			this._fillDays();
			break;
		case this._CALENDAR_MONTHS_:
			if(!this._monthsTable) this._buildMonthsTable();
			this._fillMonths();
			break;
		case this._CALENDAR_YEARS_:
			if(!this._yearsTable) this._buildYearsTable();
			this._fillYears();
			break;
	}
	this._changeModeAction(this._calendarMode,mode);
	this._setEnableData();//重设可选日期
}

Page.Calendar.prototype._changeModeAction=function(mode1,mode2){
	if(this._actioning) return;
	this._actioning=true;
	var o={};
	o[this._CALENDAR_DAYS_]={obj:this._daysTable,order:1};
	o[this._CALENDAR_MONTHS_]={obj:this._monthsTable,order:2};
	o[this._CALENDAR_YEARS_]={obj:this._yearsTable,order:3};
	
	var newElement=o[mode2].obj;
	var oldElement=o[mode1].obj;
	//改变当前的状态
	this._currentTable=newElement;
	this._calendarMode=mode2;
	//动画设置
	var moveDown=true;
	if(o[mode2].order<o[mode1].order) moveDown=false;
	var action1=new Page.Action(newElement);
	var action2=new Page.Action(oldElement);
	var action1_handler=Function.createDelegate(this,function(sender,e){
		this._actioning=false;
		action1.Remove_StopedHandler(action1_handler);
		delete newElement;newElement=null;
		delete action1_handler;
		action1_handler=null;
		o[this._CALENDAR_DAYS_].obj=null
		o[this._CALENDAR_MONTHS_].obj=null
		o[this._CALENDAR_YEARS_].obj=null
		delete o;o=null;
    	action1.dispose();
		delete action1;action1=null;
	});
	var action2_handler=Function.createDelegate(this,function(sender,e){
		oldElement.style.display="none";
		action2.Remove_StopedHandler(action2_handler);
		delete oldElement;oldElement=null;
		delete action2_handler;action2_handler=null;
		action2.dispose();
		delete action2;action2=null;
	});
	action1.Add_StopedHandler(action1_handler);
	action2.Add_StopedHandler(action2_handler);
	if(moveDown){
		document.setStyle(newElement,{left:0,top:(-oldElement.offsetHeight)+"px",display:"",position:"absolute"});
		action1.AddNumericStyleAction("top",-oldElement.offsetHeight,0,"px");
		document.setStyle(oldElement,{left:0,top:0,position:"absolute"});
		action2.AddNumericStyleAction("top",0,oldElement.offsetHeight,"px");
	}else{
		document.setStyle(newElement,{left:0,top:oldElement.offsetHeight+"px",display:"",position:"absolute"});
		action1.AddNumericStyleAction("top",oldElement.offsetHeight,0,"px");
		document.setStyle(oldElement,{left:0,top:0,position:"absolute"});
		action2.AddNumericStyleAction("top",0,-oldElement.offsetHeight,"px");
	}
	action1.StyleActionPlay(0.16);
	action2.StyleActionPlay(0.16);
}

//事件
Page.Calendar.prototype.__titleBar_mouseover=function(sender,e){
	if(this._calendarMode==this._CALENDAR_YEARS_) return;
	sender.className="titleOver";
}
Page.Calendar.prototype.__titleBar_mouseout=function(sender,e){sender.className="title";}
Page.Calendar.prototype.__titleBar_click=function(sender,e){
    if(this._showMode==this._SHOWMODE_FORINPUT_) {this._closeTimeleg.cancel();this.object.focus();}
	switch(this._calendarMode){
		case this._CALENDAR_DAYS_:
			this._changeCalenderMode(this._CALENDAR_MONTHS_);
			break;
		case this._CALENDAR_MONTHS_:
			this._changeCalenderMode(this._CALENDAR_YEARS_);
			break;
	}
}
Page.Calendar.prototype.__prevButton_mouseover=function(sender,e){}
Page.Calendar.prototype.__prevButton_mouseout=function(sender,e){}
Page.Calendar.prototype.__prevButton_click=function(sender,e){
    if(this._showMode==this._SHOWMODE_FORINPUT_) {this._closeTimeleg.cancel();this.object.focus();}
	this._prevOrNext(-1);
}
Page.Calendar.prototype.__nextButton_mouseover=function(sender,e){}
Page.Calendar.prototype.__nextButton_mouseout=function(sender,e){}
Page.Calendar.prototype.__nextButton_click=function(sender,e){
    if(this._showMode==this._SHOWMODE_FORINPUT_) {this._closeTimeleg.cancel();this.object.focus();}
	this._prevOrNext(1);
}
Page.Calendar.prototype.__footer_mouseover=function(sender,e){sender.className="footerOver";}
Page.Calendar.prototype.__footer_mouseout=function(sender,e){sender.className="footer";}
Page.Calendar.prototype.__footer_click=function(sender,e){
    if(this._showMode==this._SHOWMODE_FORINPUT_) {this._closeTimeleg.cancel();this.object.focus();}
//	this._selectedDate=this._today;
//	if(this._calendarMode!=this._CALENDAR_DAYS_){
//		this._visibleDate=new Date(this._selectedDate.getFullYear(),this._selectedDate.getMonth(),1);
//		this._changeCalenderMode(this._CALENDAR_DAYS_);
//	}else{
//		if(this._visibleDate.getFullYear()!=this._today.getFullYear()
//			||this._visibleDate.getMonth()!=this._today.getMonth()){
//			this._prevOrNextAction(new Date(this._selectedDate.getFullYear(),this._selectedDate.getMonth(),1))
//		}else this._fillDays();
//	}
//	this._onDaySelected();
}
	
Page.Calendar.prototype.__td_mouseover=function(sender,e){
    //不在可选日期范围内的
    if(!this._isEnableData(sender)) return;
	document.addClass(sender,"hover");
}
Page.Calendar.prototype.__td_mouseout=function(sender,e){
    //不在可选日期范围内的
    if(!this._isEnableData(sender)) return;
	document.removeClass(sender,"hover");
}
Page.Calendar.prototype.__td_click=function(sender,e){
    if(this._showMode==this._SHOWMODE_FORINPUT_) {this._closeTimeleg.cancel();}
    //不在可选日期范围内的
    if(!this._isEnableData(sender)) return;
	switch(this._calendarMode){
		case this._CALENDAR_DAYS_:
			this._selectedDate=sender.date;
			if(this._visibleDate.getMonth()!=this._selectedDate.getMonth()){
				this._prevOrNextAction(new Date(this._selectedDate.getFullYear(),this._selectedDate.getMonth(),1));
			}else{
			    this._fillDays();
			}
			this._onDaySelected();//触发日改变事件
			break;
		case this._CALENDAR_MONTHS_:
			this._visibleDate=new Date(sender.date.getFullYear(),sender.date.getMonth(),1);
			this._changeCalenderMode(this._CALENDAR_DAYS_);
			this._onMonthChange();//触发月改变事件
			this.object.focus();
			break;
		case this._CALENDAR_YEARS_:
			this._visibleDate=new Date(sender.date.getFullYear(),0,1);
			this._changeCalenderMode(this._CALENDAR_MONTHS_);
			this._onYearChange();//触发年改变事件
			this.object.focus();
			break;
	}
	this._setEnableData();//重设可选日期
	//this.object.focus();
}

Page.Calendar.prototype._isEnableData=function(sender){
    switch(this._calendarMode){
		case this._CALENDAR_DAYS_:
	        if(this._enableDays) if(this._enableDays.indexOf(sender.date.getDate())==-1||this._visibleDate.getMonth()!=sender.date.getMonth()) return false;
	        if(this._enableDates) if(!this._compareData(sender.date,this._enableDates,"yyyy-MM-dd")) return false;
			break;
		case this._CALENDAR_MONTHS_:
	        if(this._enableMonths) if(this._enableMonths.indexOf(sender.date.getMonth()+1)==-1||this._visibleDate.getFullYear()!=sender.date.getFullYear()) return false;
	        if(this._enableDates) if(!this._compareData(sender.date,this._enableDates,"yyyy-MM")) return false;
			break;
		case this._CALENDAR_YEARS_:
	        if(this._enableYears) if(this._enableYears.indexOf(sender.date.getFullYear())==-1) return false;
	        if(this._enableDates) if(!this._compareData(sender.date,this._enableDates,"yyyy")) return false;
			break;
    }
    return true;
}